package gang.com.greendaodemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import org.greenrobot.greendao.query.LazyList;
import org.greenrobot.greendao.query.QueryBuilder;

import java.util.Iterator;
import java.util.List;

import gang.com.greendaodemo.cachebean.User;
import gang.com.greendaodemo.greendao.EntityManager;
import gang.com.greendaodemo.greendao.UserDao;

/**
 * 官网：http://greenrobot.org/greendao/
 */
public class MainActivity extends AppCompatActivity {
    private UserDao userDao;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        QueryBuilder.LOG_SQL = true;//加入这两句可以查看到greenDao生成的sql语句
        QueryBuilder.LOG_VALUES = true;
        userDao = EntityManager.getInstance().getUserDao();
        User user1 = new User(null, "xiaogang", 20);
        userDao.insert(user1);

        //queryAll_list();
        //queryAll_listLazy();
        //queryAll_Iterator();
        //queryEq();
        //queryBetween();
        //queryLikeAsc();
    }

    /**
     * 当前查询了所有的数据-------list(直接取出所有数据并缓存数据)
     */
    public void queryAll_list() {
        //原理查询的游标就是结果集的意思，数据库中实体的数据
        List<User> list = userDao.queryBuilder().list();
        for (User user : list) {
            // Log.d("啦啦啦", "queryAll()_list called with" + user);
        }
    }

    /**
     * 当前查询所有数据-------listLazy(不直接取出数据，有缓存)
     * listLazy懒加载多个表级联查询，listLazy可以提高数据库的性能，listLazy必须关闭掉游标
     */
    public void queryAll_listLazy() {
        LazyList<User> list = userDao.queryBuilder().listLazy();
        for (User user : list) {
            //Log.d("啦啦啦", "queryAll()_listLazy called with" + list.get(0));
            //Log.d("啦啦啦", "queryAll()_listLazy called with" + user);
        }
        //关闭数据库的游标
        list.close();
    }
/**
 * 延迟加载不缓存数据----listLazyUncached
 */


    /**
     * 查询中有一个遍历的集合-----Iterator(自己遍历数据)
     * 可以根据迭代判断内容
     */
    public void queryAll_Iterator() {
        Iterator list = userDao.queryBuilder().listIterator();
        //它的遍历方法与上面的不同
        while (list.hasNext()) {
            User user = (User) list.next();
            Log.d("啦啦啦", "queryAll()_Iterator called with" + user);
        }
    }

    /**
     * 条件查询 指定内容
     */
    public void queryEq() {
        //unique只有一个值的时候才使用unique
        //User user = userDao.queryBuilder().where(UserDao.Properties.Name.eq("xiaogang")).unique();
        List user = userDao.queryBuilder().where(UserDao.Properties.Name.eq("xiaogang")).list();
        Log.d("啦啦啦", "queryEq called with" + user);
    }

    /**
     * 条件查询，like的使用,%通配符的使用查询方法
     */
    public void queryLike() {
        List data = userDao.queryBuilder().where(UserDao.Properties.Name.eq("xiaogang%")).list();
        Log.d("啦啦啦", "qqueryLike called with" + data);
    }

    /**
     * 条件查询，Between的使用,%通配符的使用查询方法
     */
    public void queryBetween() {
        List data = userDao.queryBuilder().where(UserDao.Properties.Age.between(10, 20)).list();
        Log.d("啦啦啦", "queryBetween called with" + data);
    }

    /**
     * 条件查询，>
     */
    public void queryGt() {
        List data = userDao.queryBuilder().where(UserDao.Properties.Age.gt(10)).list();
        Log.d("啦啦啦", "queryGt called with" + data);
    }

    /**
     * 条件查询，>
     */
    public void queryLt() {
        List data = userDao.queryBuilder().where(UserDao.Properties.Age.gt(20)).list();
        Log.d("啦啦啦", "queryLt called with" + data);
    }

    /**
     * 条件查询，不等于
     */
    public void queryNotEq() {
        List data = userDao.queryBuilder().where(UserDao.Properties.Age.notEq(20)).list();
        Log.d("啦啦啦", "queryNotEq called with" + data);
    }
    /**
     *  条件查询，等于空isNull、
     */

    /**
     * 条件查询，>=
     */
    public void queryGe() {
        List data = userDao.queryBuilder().where(UserDao.Properties.Age.ge(15)).list();
        Log.d("啦啦啦", "queryGe called with" + data);
    }

    /**
     * 条件查询，<=
     */
    public void queryLe() {
        List data = userDao.queryBuilder().where(UserDao.Properties.Age.le(15)).list();
        Log.d("啦啦啦", "queryLe called with" + data);
    }

    /**
     * 升序排序Asc，降序排序Desc
     */

    public void queryLikeAsc() {
        //如果以xiaogang这个姓名开头的数据，以年龄升序排列
        List data = userDao.queryBuilder().where(UserDao.Properties.Name.eq("xiaogang")).orderAsc(UserDao.Properties.Age).list();
        Log.d("啦啦啦", "queryLikeAsc called with" + data);
    }
}
